package Server;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Observable;
import java.util.Observer;
/**
 * This is the class for Login UI
 * @author wat wattanagaroon
 * @author parintorn panya
 * @version 2013/05/10
 *
 */
public class LoginUI extends JFrame {

	private JButton sethost;
	private JButton join;
	private JPanel panel;
	private JTextField ip;
	private String host;
	private String[] seperate;
	private Client client;
	private Server server;
	private JLabel label;
	private LoginUI ui;
	/**
	 * The constructor for the loginui
	 */
	public LoginUI() {
		super("Login Part");
		ui = this;
		setLocation(new Point(600, 300));
		initComponent();
		run();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	/**
	 * This is the inticomponent.
	 */
	private void initComponent() {
		sethost = new JButton("Set Host");
		sethost.setMaximumSize(new Dimension(200, 100));
		sethost.setFont(new Font("System", Font.PLAIN, 30));
		sethost.addActionListener(new SetHost());

		join = new JButton("Join server");
		join.setMaximumSize(new Dimension(200, 100));
		join.setFont(new Font("System", Font.PLAIN, 30));
		join.addActionListener(new Join());

		panel = new JPanel();

		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.add(sethost);
		panel.add(join);
		getContentPane().add(panel);

	}
	/**
	 * The run method
	 */
	public void run() {
		setSize(200, 200);
		setVisible(true);
	}
	/**
	 * The sub class that set the host
	 *
	 */
	class SetHost extends AbstractAction implements Observer {
		/**
		 * This method for the sethost
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				seperate = InetAddress.getLocalHost().toString().split("/");
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			host = seperate[1];
			try {
				server = new Server(6667);
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(null,
						"Server is created already", "WARNING",
						JOptionPane.ERROR_MESSAGE);
				System.exit(EXIT_ON_CLOSE);
			}
			try {
				client = new Client(host, 6667);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ClientUI clientui = new ClientUI(client);
			setVisible(false);
		}
		@Override
		public void update(Observable o, Object arg) {
			// TODO Auto-generated method stub

		}

	}
	/**
	 * This is the sub class for join the game.
	 *
	 */
	class Join extends AbstractAction {

		@Override
		public void actionPerformed(ActionEvent e) {

			sethost.setVisible(false);
			join.setVisible(false);
			setSize(250, 100);

			ip = new JTextField();
			ip.setColumns(15);
			ip.setPreferredSize(new Dimension(200, 30));
			ip.addKeyListener(new EnterAction());

			label = new JLabel("IP :");
			setLayout(new FlowLayout());
			add(label);
			add(ip);

		}

	}
	/**
	 * This is the subclass for the enter action
	 *
	 */
	class EnterAction implements KeyListener {

		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				ip.setForeground(Color.GREEN);
				try {
					client = new Client(ip.getText(), 6667);
				} catch (IOException ex) {
					ip.setForeground(Color.RED);
				}
				setVisible(false);
				ClientUI clientui = new ClientUI(client);
			}

		}

		@Override
		public void keyReleased(KeyEvent e) {
			ip.setForeground(Color.black);

		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub

		}

	}

}
